﻿using System;
using System.Reflection;

namespace LogoFX.Infra
{
    /// <summary>
    /// The service which generates the URI for specified resource name.
    /// </summary>
    public static class UriGenerator
    {
        /// <summary>
        /// Generates the URI for specified resource name, resource folder and resource assembly.
        /// </summary>
        /// <param name="resourceAssembly">Assembly in which the resources are.</param>
        /// <param name="resourceFolder">Resource folder.</param>
        /// <param name="resourceName">Resource name.</param>
        /// <returns>URI for specified resource name, resource folder and resource assembly.</returns>
        public static Uri GetResourceUri(Assembly resourceAssembly, string resourceFolder, string resourceName)
        {
            AssemblyName assemblyName = resourceAssembly.GetName();

            string relativePackUriForResources = String.IsNullOrEmpty(resourceFolder) ?
                String.Format("pack://application:,,,/{0};component/{1}", assemblyName.Name, resourceName) :
                String.Format("pack://application:,,,/{0};component/{1}/{2}", assemblyName.Name, resourceFolder, resourceName);

            Uri resourceLocater = new Uri(relativePackUriForResources, UriKind.Absolute);

            return resourceLocater;
        }

        /// <summary>
        /// Generates the URI for specified resource name and resource assembly.
        /// </summary>
        /// <param name="resourceAssembly">Assembly in which the resources are.</param>
        /// <param name="resourceName">Resource name.</param>
        /// <returns>URI for specified resource name, resource folder and resource assembly.</returns>
        public static Uri GetResourceUri(Assembly resourceAssembly, string resourceName)
        {
            return GetResourceUri(resourceAssembly, String.Empty, resourceName);
        }

        /// <summary>
        /// Generates the URI for specified resource name, resource folder and resource assembly (specified by type).
        /// </summary>
        /// <typeparam name="T">Type that specifies the assembly</typeparam>
        /// <param name="resourceFolder">Resource folder.</param>
        /// <param name="resourceName">Resource name.</param>
        /// <returns>URI for specified resource name, resource folder and resource assembly.</returns>
        public static Uri GetResourceUri<T>(string resourceFolder, string resourceName)
        {
            return GetResourceUri(typeof(T).
#if WinRT
                GetTypeInfo().
#endif
                Assembly, resourceFolder, resourceName);
        }

        /// <summary>
        /// Generates the URI for specified resource name and resource assembly (specified by type).
        /// </summary>
        /// <typeparam name="T">Type that specifies the assembly</typeparam>
        /// <param name="resourceName">Resource name.</param>
        /// <returns>URI for specified resource name, resource folder and resource assembly.</returns>
        public static Uri GetResourceUri<T>(string resourceName)
        {
            return GetResourceUri(typeof(T).
#if WinRT
GetTypeInfo().
#endif
                Assembly, resourceName);
        }
    }
}
